import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        redirect: '/pc/index',
    },
    {
        path: '/login',
        name: 'Name',
        component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
    },
    {
        path: '/index',
        redirect: '/pc/index',
        component: () => import(/* webpackChunkName: "about" */ '../views/home.vue'),
        children: [
            {
                path: '/pc/index',
                name: 'Index',
                component: () => import('../views/Index')
            },
            {
                path: '/pc/aicreation',
                name: 'AICreation',
                component: () => import('../views/AICreation')
            },
            {
                path: '/pc/mine',
                name: 'Mine',
                component: () => import('../views/Mine')
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

// 全局前置路由守卫
router.beforeEach(async (to, from, next) => {
    let userInfo = JSON.parse(localStorage.getItem('userInfo'));
    if (!userInfo && to.path !== '/login') {
        Message({
            message: '抱歉噢，请先登录~',
            type: 'warning'
        });
        next({ name: 'Name' })
    } else {
        if (userInfo && to.path === '/login') {
            next({ path: '/home' })
        } else {
            next()
        }

    }

    // return {path:to.path}

})

export default router;

